class Base {
	constructor(imgs) {
		this.imgs = imgs;
		this.dom = null;
		//设定血量
		this.blood = imgs.blood;
		this.createDom();
	}
	createDom() {
		this.dom = document.createElement('img');
		this.dom.src = this.imgs.baseSrc;
		this.dom.classList.add(this.imgs.className);
		//初始化样式
		const { width, height } = this.imgs;
		if (width && height) {
			this.width = width;
			this.height = height;
			this.dom.style = {
				width: width + 'px',
				height: height + 'px'
			};
		}
		$(document).ready(_ => {
			$('body').append(this.dom)
		})
	}
	changeSrc(key) {
		//切换图片
		const newSrc = this.imgs[key];
		if (newSrc) this.dom.src = newSrc;
	}
	/*****************************
	 * 检查子弹是否碰撞
	 * dLeft - bLeft < bWidth
	 * dTop - bTop < bHeight
	 * dLeft + dWidth > bLeft
	 * dTop + dHeight > bTop
	 *****************************/
	checkBeng(di, my) {
		if (di.blood <= 0) return;
		if (my.blood <= 0) return;
		if (Base.status !== 1) return;
		let dWidth = di.dom.clientWidth;
		let dHeight = di.dom.clientHeight;
		let dLeft = di.left;
		let dTop = di.top;

		let bLeft = my.left;
		let bTop = my.top;
		let bWidth = my.width;
		let bHeight = my.height;

		if (dLeft - bLeft < bWidth &&
			dTop - bTop < bHeight &&
			dLeft + dWidth > bLeft &&
			dTop + dHeight > bTop) {
			//我方血量减一
			my.blood--;
			//敌机血量减一
			di.blood--;
			//我方的飞机或子弹遭到碰撞
			my.state = false;
			//则发生碰撞后
			my.remove(di)
		}
	}
}
